﻿using System.Drawing;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ImageProcessor.Shared;
using ImageProcessor.Shared.ImageExtensions;
using ImageProcessor.Shared.Processing;
using Color = System.Windows.Media.Color;

namespace ImageProcessor.Tim
{
    public class SharpeningVM : ImageProcessorBaseVM
    {
        public static readonly DependencyProperty KProperty =
            DependencyProperty.Register("K", typeof (int), typeof (SharpeningVM), new PropertyMetadata(default(int)));

        public static readonly DependencyProperty VerticalProperty =
            DependencyProperty.Register("Vertical", typeof (bool), typeof (SharpeningVM), new PropertyMetadata(default(bool)));

        public bool Vertical
        {
            get { return (bool) GetValue(VerticalProperty); }
            set { SetValue(VerticalProperty, value); }
        }

        public int K
        {
            get { return (int) GetValue(KProperty); }
            set { SetValue(KProperty, value); }
        }

        public SharpeningVM(MainViewModel mainVM) : base(mainVM)
        {
            K = 6;
            Vertical = true;
        }

        private ImageSource _resultImage;
        
        public ImageSource ResultImage
        {
            get { return _resultImage; }
            private set
            {
                if (_resultImage == value)
                    return;
                _resultImage = value;
                OnPropertyChanged("ResultImage");
            }
        }
        
        protected override void Execute(object obj)
        {
            var bitmap = ((BitmapSource) MainVM.SourceImage).BitmapSourceToBitmap();
            var resultBitmap = (Bitmap) bitmap.Clone();

            int newB = 0;

            if (!Vertical)
            {
                //Горизонталь
                for (int y = 0; y < resultBitmap.Height; y++)
                {
                    for (int x = 255 - K; x >= 0; x--)
                    {
                        newB = resultBitmap.GetPixel(x, y).R * K;

                        for (int l = K - 1; l > 0; l--)
                            newB = newB - resultBitmap.GetPixel(x + l, y).R;
                        if (newB < 0)
                            newB = 0;
                        if (newB > 255)
                            newB = 255;
                        /*
                        if (newB < 0 || newB > 255)
                        {

                            PixelsWindows.Add(new Pixel(x, y-1, Image));
                            PixelsWindows.Add(new Pixel(x + 1, y-1, Image));
                            PixelsWindows.Add(new Pixel(x + 1, y, Image));
                            PixelsWindows.Sort((a, b) => a.Brightness.CompareTo(b.Brightness));
                            newB = PixelsWindows[1].Brightness;
                            PixelsWindows.Clear();
                        }*/
                        resultBitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(newB, newB, newB));                    
                    }
                }
            }
            else
            {
                //Вертикаль
                for (int x = 0; x < resultBitmap.Height; x++)
                {
                    for (int y = 255 - K; y >= 0; y--)
                    {
                        newB = resultBitmap.GetPixel(x, y).R*K;

                        for (int l = K - 1; l > 0; l--)
                        {
                            newB = newB - resultBitmap.GetPixel(x, y + l).R;
                        }
                        if (newB < 0)
                            newB = 0;

                        if (newB > 255)
                            newB = 255;

                        resultBitmap.SetPixel(x, y + 1, System.Drawing.Color.FromArgb(newB, newB, newB));
                        newB = 0;
                    }
                }
            }

            // TODO: выполнить алгоритм сдвига и записать результат в resultBitmap
            
            ResultImage = resultBitmap.BitmapToBitmapSource();
        }

        protected override bool CanExecute(object obj)
        {
            return MainVM.SourceImage != null;
        }
    }
}